<?php
/**
 *+-------APP 游客信息基类--------+
 *+-------------------------------------------------+
 *+------------进行用户的登录注册操作---------------+
 *+-------------------------------------------------+
 *+-@author susy.liang <liangling@ilongyuan.com.cn>-+
 *+-------------------------------------------------+
 */
namespace Api\Controller;
use Api\Controller;
use Common\Util\RSA;

class VisitorController extends PublicController {

	/**
	 *成员变量的初始化
	 */
	protected function _initialize() {
		parent::_initialize();
	}


	public function checkNickname()
	{
		$data = I('');
		$nickname = $data['nickname'];
		if (!event('User')->checkNickname($nickname)){
			return $this->error(self::$API_ERROR['NICKNAME__ILLEGAL'], '昵称不合法');
		}else if (event('User')->getUser($nickname) > 0){
			return $this->error(self::$API_ERROR['NICKNAME'], '昵称已经存在');
		}
		return $this->success('昵称可用');
	}

	/**
	 *注册
	 */
	public function register() {
		$data = I('');
		if (empty($data['imie'])) {
			return $this->error(self::$API_ERROR['PARAM'], '手机设备码不能为空');
			// $this->ajaxReturn(array('data' => null, 'code' => self::$API_ERROR['PARAM'], 'info' => '手机设备码不能为空'));
		}

		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->register($data);
		if ($res['code'] == self::$API_ERROR['SUCCESS']) {
			$this->ajaxReturn($baseObj->genToken($res['uid'], $data['imie'], $res['score'], I('platform', 0, 'intval')));
		} else {
			$this->ajaxReturn($res);
		}
	}

	/**
	 * up主工具端登录，制定用户
	 * @author liubo <liubo@ilongyuan.com.cn>
	 * @return [type] [description]
	 */
	public function uploader_login()
	{
		$data = I('');
		if (empty($data['imie'])) {
			$this->ajaxReturn(array('data' => null, 'code' => self::$API_ERROR['PARAM'], 'info' => '参数错误'));
		}
		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->login($data);
		if ($res['code'] == self::$API_ERROR['SUCCESS']) {
			$uid = $res['data']['uid'];
			$uids = C('UPLOADER_USER_IDS', null, []);
			if (!in_array($uid, $uids)) {
				$this->ajaxReturn(['code' => self::$API_ERROR['NO_USER'], 'info' => '用户不存在或被禁用！']);
			}
			$platform = I('platform', 0, 'intval');
			$this->ajaxReturn($baseObj->genToken($uid, $data['imie'], 0, $platform));

		} else {
			$this->ajaxReturn($res);
		}

	}

	/**
	 *登录
	 */
	public function login() {
		$data = I('');
		if (empty($data['imie'])) {
			$this->ajaxReturn(array('data' => null, 'code' => self::$API_ERROR['PARAM'], 'info' => '参数错误'));
		}
		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->login($data);
		if ($res['code'] == self::$API_ERROR['SUCCESS']) {
		 
			$uid = $res['data']['uid'];
			$platform = I('platform', 0, 'intval');

			$this->ajaxReturn($baseObj->genToken($uid, $data['imie'], 0, $platform));

		} else {
			$this->ajaxReturn($res);
		}
	}

	// post登录
	public function postLogin() {
		$RSA = new RSA(C('RSA_PUBLIC_KEY'), C('RSA_PRIVATE_KEY'));
		$content = file_get_contents("php://input");
		$request = $RSA->privateDecrypt($content)->toJson();
		if (!$request) {
			return $this->error(self::$API_ERROR['SIGN'], 'sign error');
		}
		$_POST = $request;
		return $this->login();
	}



	/**
	 *发送手机验证码
	 *----------------------------------
	 *------对于注册，发送前要对手机号是否已注册进行判断
	 *----------------------------------
	 */
	public function sendSms() {
		$data = I('');
		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->sendSms($data);
		$this->ajaxReturn($res);
	}

	/**
	 *发手机验证码
	 *-------------------------------
	 *-----不需要验证手机号是否已存在-------
	 *-------------------------------
	 */
	public function sendVerify() {
		$data = I('');
		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->sendSms($data, false);
		$this->ajaxReturn($res);
	}

	/**
	 *找回密码
	 */
	public function retrieve() {
		$data = I('');
		$baseObj = A('Api/User', 'Event');
		$res = $baseObj->retrieve($data);
		$this->ajaxReturn($res);
	}

	/**
	 *用户昵称列表获取
	 *根据昵称的关键字进行筛选
	 *@param string keyword 匹配的关键字
	 */
	public function nicknames() {

		$map = array();

		$keyword = I('keyword', '');
		if ($keyword) {
			$map['nickname'] = array('like', $keyword . '%');
		}

		$user = A('Api/User', 'Event')->lists($map);

		$this->ajaxReturn(array('data' => $user, 'code' => self::$API_ERROR['SUCCESS'], 'info' => '操作成功'));

	}

}